// pages/medalDetail/medalDetail.js
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
const api = require("../../config/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // index:0,
    bottomYear:'',
    bottomMonth:'',
    bottomDay:'',
    lunboData:[],
    current:0,
    types:['','坚持分类','社交达人','其他奖章'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMedalDetail()
    if(options.id){
      let id = options.id
      this.getMedalDetail(id)
    }
  },
  //获取勋章获得情况
  getMedalDetail: async function(id=0){
    const res = await request.get(api.getMyMedalAll,{})
    if(res.httpStatus==200){
      console.log('res',res);
      let lunboData = res.data
      lunboData.map((item)=>{
        item['newProgress'] = Math.floor(item.progress*100)
      })
      this.setData({
        lunboData,
        bottomYear: res.data[0].addTime?.substring(0,4),
        bottomMonth: res.data[0].addTime?.substring(5,7),
        bottomDay: res.data[0].addTime?.substring(8,10),
      })

      if(id!==0){
        res.data.map((item,index)=>{
          if(item.id==id){
            this.setData({
              current:index
            })
          }
        })
      }
    }
  },
  changeSelect:function(e){
    // console.log('e',e);
    let data = this.data.lunboData
    let index = e.detail.current
    this.setData({
      current:index,
      bottomYear: data[index].addTime?.substring(0,4),
      bottomMonth: data[index].addTime?.substring(5,7),
      bottomDay: data[index].addTime?.substring(8,10),
    })
  },

  toNext:function(){
    let {current,lunboData} = this.data
    if(current<lunboData.length-1){
      current++
      this.setData({
        current
      })
    }
  },
  toLast:function(){
    let {current,lunboData} = this.data
    if(current>0){
      current--
      this.setData({
        current
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	}
})